package android.app.betmein.activity;

import java.util.HashMap;

import viewmodel.EntitiesViewModel;
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.betmein.R;
import android.app.betmein.view.EntitiesView;
import android.content.Intent;
import android.framework.paging.BaseViewInitializer;
import android.framework.paging.SingleViewActivity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
public class EntitiesActivity extends SingleViewActivity {


	@Override
	public void init(Bundle savedInstanceState) {
		final String[] titles = getResources().getStringArray(R.array.navigation_title);
		String[] targets = getResources().getStringArray(R.array.navigation_target);
		
		final HashMap<String, Class<?>> titleToTarget = new HashMap<String, Class<?>>();
		for (int i = 0; i < targets.length; i++) {
			
			try {
				String title = titles[i];
				if(!title.equals("")){
					Class<?> cls = Class.forName(targets[i]);
					titleToTarget.put(title, cls);	
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		OnNavigationListener onNavigationListener = new OnNavigationListener() {
			public boolean onNavigationItemSelected(int position, long itemId) {
				String title = titles[position];
				if(titleToTarget.containsKey(title)){
					Class<?> cls = titleToTarget.get(title);
					Intent newActivity = new Intent(EntitiesActivity.this, cls);
					startActivity(newActivity);
					return true;	
				}
				return false;
			  }
			};
			
		SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.navigation_title,
		          android.R.layout.simple_spinner_dropdown_item);
		
		ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		actionBar.setListNavigationCallbacks(spinnerAdapter, onNavigationListener);
	}

	@Override
	public BaseViewInitializer getView() {
		EntitiesViewModel entitiesViewModel = new EntitiesViewModel(this);
        EntitiesView entitiesView = new EntitiesView(entitiesViewModel, this, R.layout.activity_entities);
        
        entitiesViewModel.setView(entitiesView);
        
        return entitiesView;
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.entities_menu, menu);
        return true;
    }
	
}
